<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">
			<el-form-item label="提现类型" prop="payoutType">
				<el-select v-model="queryParams.payoutType" placeholder="请选择提现类型" clearable>
					<el-option v-for="dict in dict.type.payout_type" :key="dict.value" :label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="提现状态" prop="payoutStatus">
				<el-select v-model="queryParams.payoutStatus" placeholder="请选择提现状态" clearable>
					<el-option v-for="dict in dict.type.payout_status" :key="dict.value" :label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-tabs v-model="activeName" type="card" @tab-click="handleClick">
			<el-tab-pane label="用户提现" name="first">
				<el-table v-loading="loading" :data="userPayoutDetailList">
					<el-table-column label="ID" align="center" prop="id" />
					<el-table-column label="用户" align="center" prop="userName" />
					<el-table-column label="提现类型" align="center" prop="payoutType">
						<template slot-scope="scope">
							<dict-tag :options="dict.type.payout_type" :value="scope.row.payoutType" />
						</template>
					</el-table-column>
					<el-table-column label="提现金额" align="center" prop="payoutPrice" />
					<el-table-column label="手续费" align="center" prop="payoutFee" />
					<el-table-column label="税费" align="center" prop="payoutTax" />
					<el-table-column label="实际到账" align="center" prop="realPrice" />
					<el-table-column label="银行卡名字" align="center" prop="backName" />
					<el-table-column label="持卡人姓名" align="center" prop="backUserName" />
					<el-table-column label="银行卡号" align="center" prop="backIdcard" />
					<el-table-column label="所属分行" align="center" prop="backBranch" />
					<el-table-column label="支付宝姓名" align="center" prop="zfbName" />
					<el-table-column label="支付宝手机号" align="center" prop="zfbTel" />

					<el-table-column label="提现状态" align="center" prop="payoutStatus">
						<template slot-scope="scope">
							<dict-tag :options="dict.type.payout_status" :value="scope.row.payoutStatus" />
						</template>
					</el-table-column>
					<el-table-column label="失败原因" align="center" prop="falseReason" />
					<el-table-column label="申请时间" align="center" prop="createTime">
						<template slot-scope="scope">
							<span>{{ parseTime(scope.row.createTime) }}</span>
						</template>
					</el-table-column>
					<el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
						<template slot-scope="scope">
							<div v-if="scope.row.payoutStatus==0">
								<el-button size="small" type="danger" plain
									@click="userRefuseButt(scope.row)">拒绝提现</el-button>
								<el-button size="small" type="success" plain
									@click="userAgreeButt(scope.row , 1)">立即提现</el-button>
							</div>
							<div v-else>---</div>
						</template>
					</el-table-column>
				</el-table>
				<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
					:limit.sync="queryParams.pageSize" @pagination="getList" />
			</el-tab-pane>

			<el-tab-pane label="技师提现申请" name="second">
				<el-table v-loading="loading" :data="technicianPayoutDetailList"
					@selection-change="handleSelectionChange">
					<el-table-column type="selection" width="55" align="center" />
					<el-table-column label="ID" align="center" prop="id" />
					<el-table-column label="提现类型" align="center" prop="payoutType">
						<template slot-scope="scope">
							<dict-tag :options="dict.type.payout_type" :value="scope.row.payoutType" />
						</template>
					</el-table-column>
					<el-table-column label="技师" align="center" prop="technicianName" />
					<el-table-column label="提现金额" align="center" prop="payoutPrice" />
					<el-table-column label="手续费" align="center" prop="premium" />
					<el-table-column label="税费" align="center" prop="payoutTax" />
					<el-table-column label="实际到账" align="center" prop="realPrice" />
					<el-table-column label="银行卡" align="center" prop="backName" />
					<el-table-column label="持卡人姓名" align="center" prop="backUserName" />
					<el-table-column label="银行卡号" align="center" prop="backIdcard" />
					<el-table-column label="所属分行" align="center" prop="backBranch" />
					<el-table-column label="支付宝姓名" align="center" prop="zfbName" />
					<el-table-column label="支付宝手机号" align="center" prop="zfbTel" />
					<el-table-column label="提现状态" align="center" prop="payoutStatus">
						<template slot-scope="scope">
							<dict-tag :options="dict.type.payout_status" :value="scope.row.payoutStatus" />
						</template>
					</el-table-column>
					<el-table-column label="失败原因" align="center" prop="falseReason" />
					<el-table-column label="申请时间" align="center" prop="createTime">
						<template slot-scope="scope">
							<span>{{ parseTime(scope.row.createTime) }}</span>
						</template>
					</el-table-column>
					<el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
						<template slot-scope="scope">
							<div v-if="scope.row.payoutStatus==0">
								<el-button size="small" type="danger" plain
									@click="userRefuseButt(scope.row)">拒绝提现</el-button>
								<el-button size="small" type="success" plain
									@click="userAgreeButt(scope.row , 2)">立即提现</el-button>
							</div>
							<div v-else>---</div>
						</template>
					</el-table-column>

					<pagination v-show="techTotal>0" :total="techTotal" :page.sync="queryParams.pageNum"
						:limit.sync="queryParams.pageSize" @pagination="techPayList" />
				</el-table>
			</el-tab-pane>
			<el-tab-pane label="业务员提现申请" name="third">
				<el-table v-loading="loading" :data="salesmanPayoutDetailList"
					@selection-change="handleSelectionChange">
					<el-table-column type="selection" width="55" align="center" />
					<el-table-column label="ID" align="center" prop="id" />
					<el-table-column label="提现类型" align="center" prop="payoutType">
						<template slot-scope="scope">
							<dict-tag :options="dict.type.payout_type" :value="scope.row.payoutType" />
						</template>
					</el-table-column>
					<el-table-column label="业务员" align="center" prop="salesmanName" />
					<el-table-column label="提现金额" align="center" prop="payoutPrice" />
					<el-table-column label="手续费" align="center" prop="payoutFee" />
					<el-table-column label="税费" align="center" prop="payoutTax" />
					<el-table-column label="实际到账" align="center" prop="realPrice" />
					<el-table-column label="银行卡名字" align="center" prop="backName" />
					<el-table-column label="持卡人姓名" align="center" prop="backUserName" />
					<el-table-column label="银行卡号" align="center" prop="backIdcard" />
					<el-table-column label="所属分行" align="center" prop="backBranch" />
					<el-table-column label="支付宝姓名" align="center" prop="zfbName" />
					<el-table-column label="支付宝手机号" align="center" prop="zfbTel" />
					<el-table-column label="提现状态" align="center" prop="payoutStatus">
						<template slot-scope="scope">
							<dict-tag :options="dict.type.payout_status" :value="scope.row.payoutStatus" />
						</template>
					</el-table-column>
					<el-table-column label="失败原因" align="center" prop="falseReason" />
					<el-table-column label="申请时间" align="center" prop="createTime">
						<template slot-scope="scope">
							<span>{{ parseTime(scope.row.createTime) }}</span>
						</template>
					</el-table-column>
					<el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
						<template slot-scope="scope">
							<div v-if="scope.row.payoutStatus==0">
								<el-button size="small" type="danger" plain
									@click="userRefuseButt(scope.row)">拒绝提现</el-button>
								<el-button size="small" type="success" plain
									@click="userAgreeButt(scope.row , 3)">立即提现</el-button>
							</div>
							<div v-else>---</div>
						</template>
					</el-table-column>
				</el-table>
				<pagination v-show="saleTotal>0" :total="saleTotal" :page.sync="queryParams.pageNum"
					:limit.sync="queryParams.pageSize" @pagination="salesList" />
			</el-tab-pane>
			<el-tab-pane label="渠道商提现申请" name="four">
				<el-table v-loading="loading" :data="channelPayoutDetailList" @selection-change="handleSelectionChange">
					<el-table-column type="selection" width="55" align="center" />
					<el-table-column label="ID" align="center" prop="id" />
					<el-table-column label="提现类型" align="center" prop="payoutType">
						<template slot-scope="scope">
							<dict-tag :options="dict.type.payout_type" :value="scope.row.payoutType" />
						</template>
					</el-table-column>
					<el-table-column label="渠道商" align="center" prop="channelName" />
					<el-table-column label="提现金额" align="center" prop="payoutPrice" />
					<el-table-column label="手续费" align="center" prop="payoutFee" />
					<el-table-column label="税费" align="center" prop="payoutTax" />
					<el-table-column label="实际到账" align="center" prop="realPrice" />
					<el-table-column label="银行卡" align="center" prop="backName" />
					<el-table-column label="持卡人姓名" align="center" prop="backUserName" />
					<el-table-column label="银行卡号" align="center" prop="backIdcard" />
					<el-table-column label="所属分行" align="center" prop="backBranch" />
					<el-table-column label="支付宝姓名" align="center" prop="zfbName" />
					<el-table-column label="支付宝手机号" align="center" prop="zfbTel" />
					<el-table-column label="提现状态" align="center" prop="payoutStatus">
						<template slot-scope="scope">
							<dict-tag :options="dict.type.payout_status" :value="scope.row.payoutStatus" />
						</template>
					</el-table-column>
					<el-table-column label="失败原因" align="center" prop="falseReason" />
					<el-table-column label="申请时间" align="center" prop="createTime">
						<template slot-scope="scope">
							<span>{{ parseTime(scope.row.createTime) }}</span>
						</template>
					</el-table-column>
					<el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
						<template slot-scope="scope">
							<div v-if="scope.row.payoutStatus==0">
								<el-button size="small" type="danger" plain
									@click="userRefuseButt(scope.row)">拒绝提现</el-button>
								<el-button size="small" type="success" plain
									@click="userAgreeButt(scope.row , 4)">立即提现</el-button>
							</div>
							<div v-else>---</div>
						</template>
					</el-table-column>
				</el-table>
				<pagination v-show="channeTotal>0" :total="channeTotal" :page.sync="queryParams.pageNum"
					:limit.sync="queryParams.pageSize" @pagination="channeList" />

			</el-tab-pane>
			<el-tab-pane label="经纪人提现申请" name="five">
				<el-table v-loading="loading" :data="brokerPayoutDetailList" @selection-change="handleSelectionChange">
					<el-table-column type="selection" width="55" align="center" />
					<el-table-column label="ID" align="center" prop="id" />
					<el-table-column label="提现类型" align="center" prop="payoutType">
						<template slot-scope="scope">
							<dict-tag :options="dict.type.payout_type" :value="scope.row.payoutType" />
						</template>
					</el-table-column>
					<el-table-column label="经纪人" align="center" prop="brokerName" />
					<el-table-column label="提现金额" align="center" prop="payoutPrice" />
					<el-table-column label="手续费" align="center" prop="payoutFee" />
					<el-table-column label="税费" align="center" prop="payoutTax" />
					<el-table-column label="实际到账" align="center" prop="realPrice" />
					<el-table-column label="银行卡名字" align="center" prop="backName" />
					<el-table-column label="持卡人姓名" align="center" prop="backUserName" />
					<el-table-column label="银行卡号" align="center" prop="backIdcard" />
					<el-table-column label="所属分行" align="center" prop="backBranch" />
					<el-table-column label="支付宝姓名" align="center" prop="zfbName" />
					<el-table-column label="支付宝手机号" align="center" prop="zfbTel" />
					<el-table-column label="提现状态" align="center" prop="payoutStatus">
						<template slot-scope="scope">
							<dict-tag :options="dict.type.payout_status" :value="scope.row.payoutStatus" />
						</template>
					</el-table-column>
					<el-table-column label="失败原因" align="center" prop="falseReason" />
					<el-table-column label="申请时间" align="center" prop="createTime">
						<template slot-scope="scope">
							<span>{{ parseTime(scope.row.createTime) }}</span>
						</template>
					</el-table-column>
					<el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
						<template slot-scope="scope">
							<div v-if="scope.row.payoutStatus==0">
								<el-button size="small" type="danger" plain
									@click="userRefuseButt(scope.row)">拒绝提现</el-button>
								<el-button size="small" type="success" plain
									@click="userAgreeButt(scope.row , 5)">立即提现</el-button>
							</div>
							<div v-else>---</div>
						</template>
					</el-table-column>
				</el-table>
				<pagination v-show="brokerTotal>0" :total="brokerTotal" :page.sync="queryParams.pageNum"
					:limit.sync="queryParams.pageSize" @pagination="brokerList" />
			</el-tab-pane>
		</el-tabs>


		<!-- 添加或修改用户提现申请对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="600px" append-to-body center>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="拒绝原因" prop="refuseReason">
					<el-input v-model="form.refuseReason" placeholder="请输入拒绝原因" />
				</el-form-item>
			</el-form>

			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="userRefuseDig" class="elBut">提 交</el-button>
				<el-button @click="cancel" class="elBut">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listUserPayoutDetail,
		getUserPayoutDetail,
		delUserPayoutDetail,
		addUserPayoutDetail,
		updateUserPayoutDetail,
		userAgreePayout,
		userRefusePayout,
		technicianRefusePayout,
		technicianAgreePayout,
		salesmanRefusePayout,
		salesmanAgreePayout,
		channelRefusePayout,
		channelAgreePayout,
		brokerRefusePayout,
		brokerAgreePayout
	} from "@/api/userPayoutDetail/userPayoutDetail";
	import {
		listTechnicianPayoutDetail
	} from "@/api/technicianPayoutDetail/technicianPayoutDetail";
	import {
		listSalesmanPayoutDetail
	} from "@/api/salesmanPayoutDetail/salesmanPayoutDetail";
	import {
		listBrokerPayoutDetail
	} from "@/api/brokerPayoutDetail/brokerPayoutDetail";
	import {
		listChannelPayoutDetail
	} from "@/api/channelPayoutDetail/channelPayoutDetail";
	export default {
		name: "UserPayoutDetail",
		dicts: ['payout_type', 'payout_status'],
		data() {
			return {
				refuseId: '',
				activeName: 'first',
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 用户提现申请表格数据
				userPayoutDetailList: [],
				techTotal: 0,
				// 技师提现申请表格数据
				technicianPayoutDetailList: [],
				channeTotal: 0,
				// 渠道商提现申请表格数据
				channelPayoutDetailList: [],
				saleTotal: 0,
				// 业务员提现申请表格数据
				salesmanPayoutDetailList: [],
				brokerTotal: 0,
				// 经纪人提现记录表格数据
				brokerPayoutDetailList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					payoutType: null,
					payoutStatus: null,
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {}
			};
		},
		created() {
			if (this.activeName == 'first') this.getList()
		},
		methods: {
			/** 查询用户提现申请列表 */
			getList() {
				this.loading = true;
				listUserPayoutDetail(this.queryParams).then(response => {
					this.userPayoutDetailList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			/** 查询技师提现申请列表 */
			techPayList() {
				this.loading = true;
				listTechnicianPayoutDetail(this.queryParams).then(response => {
					this.technicianPayoutDetailList = response.rows;
					this.techTotal = response.total;
					this.loading = false;
				});
			},
			/** 查询业务员提现申请列表 */
			salesList() {
				this.loading = true;
				listSalesmanPayoutDetail(this.queryParams).then(response => {
					this.salesmanPayoutDetailList = response.rows;
					this.saleTotal = response.total;
					this.loading = false;
				});
			},
			/** 查询渠道商提现申请列表 */
			channeList() {
				this.loading = true;
				listChannelPayoutDetail(this.queryParams).then(response => {
					this.channelPayoutDetailList = response.rows;
					this.channeTotal = response.total;
					this.loading = false;
				});
			},
			/** 查询经纪人提现记录列表 */
			brokerList() {
				this.loading = true;
				listBrokerPayoutDetail(this.queryParams).then(response => {
					this.brokerPayoutDetailList = response.rows;
					this.brokerTotal = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.refuseId = ''
				this.form = {
					id: null,
					userId: null,
					payoutType: null,
					payoutPrice: null,
					payoutFee: null,
					payoutTax: null,
					realPrice: null,
					backName: null,
					backUserName: null,
					backIdcard: null,
					backBranch: null,
					payoutStatus: null,
					falseReason: null,
					isDelete: null,
					createTime: null,
					updateTime: null
				};
				this.resetForm("form");
			},
			queryReset() {
				this.refuseId = ''
				this.queryParams = {
					pageNum: 1,
					pageSize: 10,
					payoutType: null,
					payoutStatus: null,
				}
			},

			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				if (this.activeName == 'first') this.getList()
				if (this.activeName == 'second') this.techPayList()
				if (this.activeName == 'third') this.salesList()
				if (this.activeName == 'four') this.channeList()
				if (this.activeName == 'five') this.brokerList()
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			userRefuseButt(row) {
				this.reset();
				this.open = true;
				this.title = "拒绝理由";
				this.refuseId = row.id
			},
			userRefuseDig() {
				let payOut 
				if (this.activeName == 'first') payOut = userRefusePayout
				if (this.activeName == 'second') payOut = technicianRefusePayout
				if (this.activeName == 'third') payOut = salesmanRefusePayout
				if (this.activeName == 'four') payOut = channelRefusePayout
				if (this.activeName == 'five') payOut = brokerRefusePayout
				payOut({
					id: this.refuseId,
					refuseReason: this.form.refuseReason
				}).then(response => {
					this.open = false;
					this.reset();
					this.$modal.msgSuccess("拒绝提现");
					if (this.activeName == 'first') this.getList()
					if (this.activeName == 'second') this.techPayList()
					if (this.activeName == 'third') this.salesList()
					if (this.activeName == 'four') this.channeList()
					if (this.activeName == 'five') this.brokerList()
				});
			},
			userAgreeButt(row, index) {
				const id = row.id || this.ids
				let payOut
				if(index==1)payOut = userAgreePayout
				if(index==2)payOut = technicianAgreePayout
				if(index==3)payOut = salesmanAgreePayout
				if(index==4)payOut = channelAgreePayout
				if(index==5)payOut = brokerAgreePayout
				
				this.$modal.confirm('是否同意提现').then(function() {
					return payOut({
						id: id
					});
				}).then(() => {
					this.$modal.msgSuccess("处理成功");
					if (this.activeName == 'first') this.getList()
					if (this.activeName == 'second') this.techPayList()
					if (this.activeName == 'third') this.salesList()
					if (this.activeName == 'four') this.channeList()
					if (this.activeName == 'five') this.brokerList()
				}).catch(() => {});
			},
			// 标签点击
			handleClick(tab, event) {
				this.activeName = tab.name
				this.queryReset()
				if (this.activeName == 'first') this.getList()
				if (this.activeName == 'second') this.techPayList()
				if (this.activeName == 'third') this.salesList()
				if (this.activeName == 'four') this.channeList()
				if (this.activeName == 'five') this.brokerList()
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加用户提现申请";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const id = row.id || this.ids
				getUserPayoutDetail(id).then(response => {
					this.form = response.data;
					this.open = true;
					this.title = "修改用户提现申请";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.id != null) {
							updateUserPayoutDetail(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addUserPayoutDetail(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$modal.confirm('是否确认删除用户提现申请编号为"' + ids + '"的数据项？').then(function() {
					return delUserPayoutDetail(ids);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('userPayoutDetail/userPayoutDetail/export', {
					...this.queryParams
				}, `userPayoutDetail_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>

<style lang="less">
	.el-button {
		margin: 4px 0;
	}
	.el-button + .el-button {
	    margin-left: 0px;
	}
</style>